import * as apis from '@/services/ReturnMoneyValueMgr'; // 回款系数
import * as fun from './constant';
import _ from 'lodash';

// 正式版
export const getFormalData = async id => {
  const res = await apis.returnCoefficientDetailUsingGET({ params: { versionId: id } });
  if (!res || res.code !== 1) {
    return { columns: [], data: [] };
  }
  const { columns, data } = fun.resetData(res.data);
  return { columns, data };
};
// 调整版
export const getAdjustmentData = async id => {
  const res = await apis.returnCoefficientDetailUsingGET({ params: { versionId: id } });
  if (!res || res.code !== 1) {
    return { columns: [], data: [] };
  }
  const { columns, data } = fun.resetData(res.data);
  return { columns, data };
};
// 调整版编辑
export const getAdjustmentEditData = async id => {
  const res = await apis.queryDetailUsingGET({ params: { versionId: id } });
  if (!res || res.code !== 1) {
    return { columns: [], data: [] };
  }
  const rawData = _.cloneDeep(res.data);
  const { columns, data, footerData } = fun.resetData(res.data);
  return { columns, data, rawData, footerData };
};
// 调整版保存
export const insertEditData = async (param, callback) => {
  const res = await apis.createSupplyBudgetVersionUsingPUT({ data: param });
  if (!res || res.code !== 1) return false;
  if (callback) callback();
};
// 调整版删除
export const deleteVersion = async versionId => {
  const res = await apis.delUsingDELETE({ params: { versionId } });
  if (!res || res.code !== 1) return false;
  return true;
};
// 实际版
export const getActualData = async id => {
  const res = await apis.returnCoefficientDetailUsingGET({ params: { versionId: id } });
  // const res = await createData2({ params: { versionId: id } });
  if (!res || res.code !== 1) {
    return { columns: [], data: [] };
  }
  const { columns, data } = fun.resetDataPractical(res.data);
  return { columns, data };
};
// 审批接口数据
export const getApproveData = async ({ vId, pId, examineId }) => {
  let params = {};
  if (examineId) {
    params = { examineId };
  } else {
    params = { versionId: vId, projectId: pId };
  }
  const res = await apis.getApprovalDetailUsingGET({ params });
  if (res && res.code === 1 && !_.isEmpty(res.data) && res.data !== 'null') {
    const { data, files, description, basicInfo } = fun.resetApproveData(res.data);
    return { data, files, description, basicInfo, code: 1 };
  }
};
// app审批接口数据
export const getAppApproveData = async ({ examineId }) => {
  const res = await apis.approvalPreviewAppUsingGET({ params: { examineId } });
  if (res && res.code === 1 && !_.isEmpty(res.data) && res.data !== 'null') {
    const { data, files, description, basicInfo } = fun.resetApproveData(res.data);
    return { data, files, description, basicInfo, code: 1 };
  }
};
